cc.Class({
    extends: cc.Component,

    properties: {
        motors: require('Motors'),
        desert: require('Desert'),
        placeTip: require('PlaceTip'),
        readyGo: cc.Prefab,
        minRoads: [cc.Sprite],
        minMotors: [cc.Sprite],
    },

    onLoad() {
        this.desert.node.setLocalZOrder(-100);
        this.isFinish = true;
        this.updateX = 0;
        this.speedX = 0;
        for(var i in this.minRoads) this.minRoads[i].node.active = false;
    },

    loadMotors() {
        if(this.motors.motors) return;
        this.motors.loadMotors();
        this.motors.node.on('MOVE_CAPTURE', this.onMoveCapture, this);
        this.readyGo = cc.instantiate(this.readyGo).getComponent(cc.Animation);
        this.readyGo.node.parent = this.node;
        this.readyGo.node.x = 10000;
        this.readyGo.node.on('READY_GO', () => this.onMoveStart(), this);
    },

    showMotors(show) {
        !show && (this.isFinish = GLOBAL.IS_MOVE_END = true);
        this.motors.node.y = show ? 0 : 10000;
        this.node.find('MotorsMap').active = show;
    },

    onMoveReady() {
        this.loadMotors();
        this.showMotors(true);
        this.motors.onMoveReady();
        this.readyGo.node.x = 0;
        this.readyGo.play();
        cc.Sound.pauseMusic('snd_bg');
        cc.Sound.playEffect('snd_ready');
        for(var i in this.minRoads) this.minRoads[i].node.active = false;
    },

    onMoveStart(timer) {
        this.speedX = 4;
        this.updateX = 0;
        this.isFinish = GLOBAL.IS_MOVE_END = false;
        this.loadMotors();
        this.showMotors(true);
        this.desert.onMoveStart();
        this.motors.onMoveStart(GLOBAL.gameResult.bikes, timer);
        var bikes = GLOBAL.gameState.bikes;
        for(var i in bikes) {
            var id = bikes[i].id - 1;
            if(bikes[i].isBet) {
                this.motors.showSelect(id);
                this.minRoads[id].node.active = true;
            }
        }
        if(!timer) {
            cc.Sound.playEffect('snd_go');
            cc.Sound.playEffect('snd_gun');
            cc.Sound.playMusic('snd_run');
        };
    },

    onMoveCapture() {
        this.speedX = 1;
        cc.Sound.playEffect('snd_cheer');
        cc.Effect.shotScreen(this.node, () => {
            this.isFinish = GLOBAL.IS_MOVE_END = true;
            this.motors.onMoveSprint();
            this.scheduleOnce(() => this.node.emit('MOVE_FINISH'), 1);
        });
    },

    lateUpdate(dt) {
        this.updateMinMotors();
        if(!this.isFinish) {
            var ms = this.orderMotorsX();
            var offsetx = ms[0].node.x - 800;
            if(offsetx > this.updateX) this.updateX = offsetx;
            else this.updateX += this.speedX;
            this.motors.updateX(this.updateX); 
            this.desert.updateX(this.updateX);
            this.placeTip.updateTip(ms[0]);
        }
    },

    updateMinMotors() {
        var ms = this.motors.motors;
        if(ms) {
            for(var i = 0; i < ms.length; i++) { //-330 ==> 350
                var minx = -330 + ms[i].node.x / (GLOBAL.TOTAL_MOVE * GLOBAL.FACTOR_MOVE) * 680;
                this.minMotors[i].node.x = Math.min(minx, 350);
                this.motors.updateSelect(i, ms[i].node.x);
            }
        }
    },

    orderMotorsX() {
        var ms = this.motors.motors.concat();
        for(var i = 0; i < ms.length; i++) {
            for(var j = i; j < ms.length; j++) {
                if(ms[j].node.x > ms[i].node.x) {
                    var t = ms[j];
                    ms[j] = ms[i];
                    ms[i] = t;
                }
            }
        }
        return ms;
    }
});
